﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PM.Web.Admin
{
    public partial class admin_p_know_addup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["action"]))
                {
                    if (Request.QueryString["action"] == "del")
                    {
                        delData();
                    }
                }
                bindDDL();
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    this.hidId.Value = Request.QueryString["id"];
                    bindData();
                }
            }
        }

        private void bindDDL()
        {
            DataAccess.PmData dal = new DataAccess.PmData();
            this.ddlComList.DataSource = dal.GetCommunity(int.Parse(Session["CommId"].ToString()), true);
            this.ddlComList.DataBind();
        }

        private void delData()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                DataAccess.PmData dal = new DataAccess.PmData();
                if (dal.Del_p_Know(int.Parse(Request.QueryString["id"])))
                {
                    Response.Write("ok");
                }
                else
                {
                    Response.Write("err");
                }
            }
            Response.End();
        }

        private void bindData()
        {
            if (this.hidId.Value != "0")
            {

                DataAccess.PmData dal = new DataAccess.PmData();
                System.Data.DataTable dt = dal.Get_p_know(int.Parse(this.hidId.Value), null);
                if (dt.Rows.Count > 0)
                {
                    this.ddlComList.Items.FindByValue(dt.Rows[0]["Self_Community_Code"].ToString()).Selected = true;
                    this.txtProjNo.Text = dt.Rows[0]["ProjNo"].ToString();
                    this.txtProjDate1.Text = dt.Rows[0]["ProjDate1"].ToString();
                    this.txtProjName1.Text = dt.Rows[0]["ProjName1"].ToString();
                    this.txtProjTel.Text = dt.Rows[0]["ProjTel"].ToString();
                    this.txtProjWillDate.Text = dt.Rows[0]["ProjWillDate"].ToString();
                    this.txtProjEndDate.Text = dt.Rows[0]["ProjEndDate"].ToString();
                    this.ddlProjType.Items.FindByValue(dt.Rows[0]["ProjType"].ToString()).Selected = true;
                    this.ddlProjSize.Items.FindByValue(dt.Rows[0]["ProjSize"].ToString()).Selected = true;
                    this.txtProj_Name.Text = dt.Rows[0]["Proj_Name"].ToString();
                    this.txtProjOrder.Text = dt.Rows[0]["ProjOrder"].ToString();
                    this.txtProjAddress1.Text = dt.Rows[0]["ProjAddress1"].ToString();
                    this.txtProjReport1.Text = dt.Rows[0]["ProjReport1"].ToString();
                    this.txtProjPay1.Text = dt.Rows[0]["ProjPay1"].ToString();
                    this.ddlProjPaySource1.Items.FindByValue(dt.Rows[0]["ProjPaySource1"].ToString()).Selected = true;
                    this.txtProjMens1.Text = dt.Rows[0]["ProjMens1"].ToString();
                    this.txtProjMan1.Text = dt.Rows[0]["ProjMan1"].ToString();
                    this.txtProjRecoName1.Text = dt.Rows[0]["ProjRecoName1"].ToString();
                    this.txtProjRecoDate1.Text = dt.Rows[0]["ProjRecoDate1"].ToString();
                    this.editor_Proj_Remark1.InnerText = dt.Rows[0]["Proj_Remark1"].ToString();
                    this.txtProjDate2.Text = dt.Rows[0]["ProjDate2"].ToString();
                    this.txtProjLeader.Text = dt.Rows[0]["ProjLeader"].ToString();
                    this.txtProjPay2.Text = dt.Rows[0]["ProjPay2"].ToString(); 
                    this.ddlProjPaySource2.Items.FindByValue(dt.Rows[0]["ProjPaySource2"].ToString()).Selected = true;
                    this.txtProjMens2.Text = dt.Rows[0]["ProjMens2"].ToString();
                    this.txtProjMan2.Text = dt.Rows[0]["ProjMan2"].ToString();
                    this.txtProjRecoName2.Text = dt.Rows[0]["ProjRecoName2"].ToString();
                    this.txtProjRecoDate2.Text = dt.Rows[0]["ProjRecoDate2"].ToString();
                    this.txtProjAddress2.Text = dt.Rows[0]["ProjAddress2"].ToString();
                    this.editor_Proj_Remark2.InnerText = dt.Rows[0]["Proj_Remark2"].ToString();

                    this.txtProjPay3.Text = dt.Rows[0]["ProjPay3"].ToString();
                    this.txtProjMens3.Text = dt.Rows[0]["ProjMens3"].ToString();
                    this.txtProjMan3.Text = dt.Rows[0]["ProjMan3"].ToString();
                    this.ddlProjPaySource3.Items.FindByValue(dt.Rows[0]["ProjPaySource3"].ToString()).Selected = true;
                    this.ddlProjSaid.Items.FindByValue(dt.Rows[0]["ProjSaid"].ToString()).Selected = true;
                    this.txtProjRecoName3.Text = dt.Rows[0]["ProjRecoName3"].ToString();
                    this.txtProjRecoDate3.Text = dt.Rows[0]["ProjRecoDate3"].ToString();

                    this.editor_Proj_Remark3.InnerText = dt.Rows[0]["Proj_Remark3"].ToString();
                    this.txtRemark.Text = dt.Rows[0]["Remark"].ToString();
                    this.radProjState1.Checked = (dt.Rows[0]["ProjState"].ToString() == radProjState1.Text);
                    this.radProjState2.Checked = (dt.Rows[0]["ProjState"].ToString() == radProjState2.Text);
                    this.radProjState3.Checked = (dt.Rows[0]["ProjState"].ToString() == radProjState3.Text);

                }
            }
        }

        protected void cmdSave_Click(object sender, EventArgs e)
        {
            string Self_Community = this.ddlComList.SelectedValue;
            string ProjNo = this.txtProjNo.Text.Trim();
            string ProjDate1 = this.txtProjDate1.Text.Trim();
            string ProjName1 = this.txtProjName1.Text.Trim();
            string ProjTel = this.txtProjTel.Text.Trim();
            string ProjWillDate = this.txtProjWillDate.Text.Trim();
            string ProjEndDate = this.txtProjEndDate.Text.Trim();
            string ProjType = this.ddlProjType.SelectedValue;
            string ProjSize = this.ddlProjSize.SelectedValue;
            string Proj_Name = this.txtProj_Name.Text.Trim();
            string ProjOrder = this.txtProjOrder.Text.Trim();
            string ProjAddress1 = this.txtProjAddress1.Text.Trim();
            string ProjReport1 = this.txtProjReport1.Text.Trim();
            decimal ProjPay1 = decimal.Parse(this.txtProjPay1.Text.Trim());
            string ProjPaySource1 = this.ddlProjPaySource1.SelectedValue;
            decimal ProjMens1 = decimal.Parse(this.txtProjMens1.Text.Trim());
            decimal ProjMan1 = decimal.Parse(this.txtProjMan1.Text.Trim());
            string ProjRecoName1 = this.txtProjRecoName1.Text.Trim();
            string ProjRecoDate1 = this.txtProjRecoDate1.Text.Trim();
            string Proj_Remark1 = this.editor_Proj_Remark1.InnerText.Trim();
            string ProjDate2 = this.txtProjDate2.Text.Trim();
            string ProjLeader = this.txtProjLeader.Text.Trim();
            decimal ProjPay2 = decimal.Parse(this.txtProjPay2.Text.Trim());
            string ProjPaySource2 = this.ddlProjPaySource2.SelectedValue;
            decimal ProjMens2 = decimal.Parse(this.txtProjMens2.Text.Trim());
            decimal ProjMan2 = decimal.Parse(this.txtProjMan2.Text.Trim());
            string ProjRecoName2 = this.txtProjRecoName2.Text.Trim();
            string ProjRecoDate2 = this.txtProjRecoDate2.Text.Trim();
            string ProjAddress2 = this.txtProjAddress2.Text.Trim();
            string Proj_Remark2 = this.editor_Proj_Remark2.InnerText.Trim();

            decimal ProjPay3 = decimal.Parse(this.txtProjPay3.Text.Trim());
            decimal ProjMens3 = decimal.Parse(this.txtProjMens3.Text.Trim());
            decimal ProjMan3 = decimal.Parse(this.txtProjMan3.Text.Trim());
            string ProjPaySource3 = this.ddlProjPaySource3.SelectedValue;
            string ProjSaid = this.ddlProjSaid.SelectedValue;
            string ProjRecoName3 = this.txtProjRecoName3.Text.Trim();
            string ProjRecoDate3 = this.txtProjRecoDate3.Text.Trim();
            string Proj_Remark3 = this.editor_Proj_Remark3.InnerText.Trim();
            string Remark = this.txtRemark.Text.Trim(); 


            string ProjState;
                 if (this.radProjState1.Checked == true)
                    ProjState = this.radProjState1.Text.Trim();
                 else
                 if (this.radProjState2.Checked == true)
                    ProjState = this.radProjState2.Text.Trim();
                 else 
                    ProjState = this.radProjState3.Text.Trim();

            DateTime Checkdate;
            if (!DateTime.TryParse(ProjDate1, out Checkdate)) { PM.Web.Common.JsAlert(this, "请输入正确的策划日期!"); return; }
            if (!DateTime.TryParse(ProjWillDate, out Checkdate)) { PM.Web.Common.JsAlert(this, "请输入正确的计划举办日期!"); return; }
            if (!DateTime.TryParse(ProjEndDate, out Checkdate)) { PM.Web.Common.JsAlert(this, "请输入正确的计划截止日期!"); return; }
            if (!DateTime.TryParse(ProjRecoDate1, out Checkdate)) { PM.Web.Common.JsAlert(this, "请输入正确的策划记录日期日期!"); return; }
            if (!DateTime.TryParse(ProjRecoDate2, out Checkdate)) { PM.Web.Common.JsAlert(this, "请输入正确的活动记录日期!"); return; }
            if (!DateTime.TryParse(ProjRecoDate3, out Checkdate)) { PM.Web.Common.JsAlert(this, "请输入正确的活动总结日期!"); return; }

             

            DataAccess.PmData dal = new DataAccess.PmData();
            if (hidId.Value == "0")
            {
                if (dal.Add_face_m_unit_p_know(int.Parse(Self_Community), ProjNo,ProjName1, DateTime.Parse(ProjDate1), DateTime.Parse(ProjWillDate),  DateTime.Parse(ProjEndDate),ProjType, Proj_Name,
                                            ProjSize,ProjOrder,ProjPaySource1,ProjTel,ProjAddress1,ProjReport1,ProjPay1,ProjRecoName1,ProjMens1,ProjMan1, DateTime.Parse(ProjRecoDate1),Proj_Remark1,Proj_Remark2,
                                            Proj_Remark3, ProjAddress2, DateTime.Parse(ProjDate2), ProjLeader, ProjPay2, ProjPaySource2, ProjRecoName2, ProjMens2, ProjMan2, DateTime.Parse(ProjRecoDate2), ProjSaid, ProjPay3,
                                            ProjPaySource3,ProjRecoName3, ProjMens3,ProjMan3,DateTime.Parse(ProjRecoDate3),ProjState,Remark,DateTime.Now,Session["Name"].ToString()))
               
                
                {
                    PM.Web.Common.JsAlert(this, "保存成功！", true);
                }
            }
            else
            {
                if (dal.Modify_face_m_unit_p_know(int.Parse(hidId.Value),int.Parse(Self_Community), ProjNo, ProjName1, DateTime.Parse(ProjDate1), DateTime.Parse(ProjWillDate), DateTime.Parse(ProjEndDate), ProjType, Proj_Name,
                                            ProjSize, ProjOrder, ProjPaySource1, ProjTel, ProjAddress1, ProjReport1, ProjPay1, ProjRecoName1, ProjMens1, ProjMan1, DateTime.Parse(ProjRecoDate1), Proj_Remark1, Proj_Remark2,
                                            Proj_Remark3, ProjAddress2, DateTime.Parse(ProjDate2), ProjLeader, ProjPay2, ProjPaySource2, ProjRecoName2, ProjMens2, ProjMan2, DateTime.Parse(ProjRecoDate2), ProjSaid, ProjPay3,
                                            ProjPaySource3, ProjRecoName3, ProjMens3, ProjMan3, DateTime.Parse(ProjRecoDate3), ProjState, Remark, DateTime.Now, Session["Name"].ToString()))
                {
                    PM.Web.Common.JsAlert(this, "修改成功！", true);
                }
            }
        }
         

    }
}